﻿using MK;
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace AutoEditUI
{
    internal class MBXUtils
    {
        public static bool ExportToPic(string tmp, Canvas surface, double MBLConfigureDPITimes)
        {

            Uri xUri = new Uri(tmp, UriKind.Absolute);
            try
            {
                if (xUri == null)
                {
                    LogHelper.Log("ExportToPic path null");
                    return false;
                }
                double x = MBLConfigureDPITimes;
                Size size = new Size(surface.ActualWidth, surface.ActualHeight);
                surface.Measure(size);
                surface.Arrange(new System.Windows.Rect(size));
                RenderTargetBitmap renderBitmap =
                  new RenderTargetBitmap(
                     (int)(surface.ActualWidth * x),
                    (int)(surface.ActualHeight * x),
                    96 * x,
                    96 * x,
                    System.Windows.Media.PixelFormats.Pbgra32);

                renderBitmap.Render(surface);

                using (FileStream outStream = new FileStream(xUri.LocalPath, FileMode.Create))
                {
                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
                    encoder.Save(outStream);
                }
                LogHelper.Log(" ExportToPic Finish " + xUri);
                return true;
            }
            catch (Exception eee)
            {
                LogHelper.Log(" ExportToPic Exception " + eee.ToString());
                return false;
            }
        }
    }
}